# -*- coding:utf-8 -*-

class Node:
    def __init__(self, data=None):
        '''数据结构：链表节点
        :param data: 存储数据
        '''
        self.data = data
        
        # 双向循环链表
        self.prev = None # 单向链表不需要这个属性
        self.next = None

class Linked:
    """循环链表
    """
    def __init__(self):
        """链表结构
        """
        self.link = None # 存放节点、子链, 即head

        self.__size__ = 0 # 链表大小、尺寸
    
    @property
    def data(self):
        if self.link is None:
            return None
        else:
            return self.link.data

    @property
    def size(self):
        return self.__size__

    def append(self, data:any):
        node = Node(data)
        if self.link is None:
            self.link = node
            # 构成环
            node.prev = node
            node.next = node
        else:
            node.prev = self.link.prev
            node.next = self.link
            self.link.prev.next = node # 环，起始节点的上一个节点就是尾节点
            self.link.prev = node # 追加，尾节点变为新加入节点
        self.__size__ += 1
        
    def remove(self, data):
        """移除第一个data = data的节点
        """
        node = self.search(data)

        if node == None:
            return None
        else:
            if self.size == 1:
                self.link = None
                self.__size__ = 0
            else:
                node.prev.next = node.next
                node.next.prev = node.prev

                if node == self.link:
                    self.link = node.next

                self.__size__ -= 1
            return node

    def datas(self):
        items = list()
        if self.link is not None:
            node = self.link
            for i in range(self.size):
                items.append(node.data)
                node = node.next
        return items

    def search(self, data):
        node = self.link
        while node.data != data:
            node = node.next
            if node == self.link:
                return None
        return node

def nodes(link:Linked):
    if link is None:
        return []
    elif link.link is None:
        return []
    else:
        return []
